/**
 * Copyright (c) 2017-2020 四川智慧云天 All rights reserved.
 * https://www.sczhyt.cn
 * 版权所有，侵权必究！
 */
package cn.sczhyt.modules.sys.controller;

import cn.sczhyt.common.annotation.SysLog;
import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.R;
import cn.sczhyt.common.validator.ValidatorUtils;
import cn.sczhyt.modules.sys.entity.SysDictEntity;
import cn.sczhyt.modules.sys.entity.SysUserEntity;
import cn.sczhyt.modules.sys.service.SysDictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 数据字典
 *
 * @author Mark sunlightcs@gmail.com
 */
@Api(tags = "字典管理")
@RestController
@RequestMapping("sys/dict")
public class SysDictController extends AbstractController {
    @Autowired
    private SysDictService sysDictService;

    /**
     * 列表
     * 分页，条件，排序
     */
    @ApiOperation(value = "列表")
    @GetMapping("/list")
    //@RequiresPermissions("sys:dict:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysDictService.queryPage(params);
        return R.ok().put("data", page);
    }

    /**
     * 具体某一类的数据字典
     */
    @ApiOperation(value = "字典")
    @GetMapping("/SelectByType")
    //@RequiresPermissions("sys:dict:list")
    public R SelectByType(@RequestParam String type) {
        QueryWrapper<SysDictEntity> wrapper = new QueryWrapper<>();
        List<SysDictEntity> list = sysDictService.list(wrapper.eq("type",type));

        return R.ok().put("data", list);
    }
    /**
     * 数据字典分类
     */
    @ApiOperation(value = "字典")
    @GetMapping("/listType")
    //@RequiresPermissions("sys:dict:list")
    public R SelectType() {
        QueryWrapper<SysDictEntity> wrapper = new QueryWrapper<>();
        wrapper.select("type","code");
        List<SysDictEntity> list = sysDictService.list(wrapper.groupBy("type"));
        return R.ok().put("data", list);
    }
    /**
     * 信息
     */
    @ApiOperation(value = "信息")
    @GetMapping(value = "/info/{id}")
    //@RequiresPermissions("sys:dict:info")
    public R info(@ApiParam(value = "用户名", required = true) @PathVariable("id") String id) {
        SysDictEntity dict = sysDictService.getById(id);
        return R.ok().put("data", dict);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @SysLog("保存数据字典")
    @PostMapping("/save")
    @RequiresPermissions("sys:dict:save")
    public R save(@RequestBody SysDictEntity dict) {
        //校验类型
        ValidatorUtils.validateEntity(dict);
//        if(dict.getId()==null || dict.getId().equals("")){
//            sysDictService.save(dict);
//        }else {
//            update(dict);
//        }
        sysDictService.Save(dict);
        return R.ok().put("data", dict);
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @SysLog("修改数据字典")
    @PostMapping("/update")
    @RequiresPermissions("sys:dict:update")
    public R update(@RequestBody SysDictEntity dict) {
        sysDictService.updateById(dict);
        return R.ok().put("data", dict);
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @SysLog("删除数据字典")
    @PostMapping("/delete")
    @RequiresPermissions("sys:dict:delete")
    public R delete(@RequestBody String[] ids) {
        sysDictService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 建议登记页面的固定内容（提案类型，内容分类，届次）
     * 删除
     */
    @ApiOperation("字典类型")
    @PostMapping("/type")
    //@RequiresPermissions("sys:dict:delete")
    public R dictType() {

        List<SysDictEntity> entityList = sysDictService.list(new QueryWrapper<SysDictEntity>().groupBy("type").select("code", "type"));
        return R.ok();
    }
}
